﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TpOffice.Controller;
using TpOffice.Interfaces;
using TecBaseObjects.Models;
using TecBaseObjects.Objects;
using TecExtensions;


namespace TpOffice.Forms {

    public partial class CashSaleDetailForm : Form, ITecForm {

        #region Members

        private CashSaleDetailController controller;
        private int myCashSaleID;
        private int paymentMode;

        #endregion

        #region Constructor

        public CashSaleDetailForm(int cashSaleID) {
            InitializeComponent();
            if (cashSaleID > 0) {
                this.myCashSaleID = cashSaleID;
            }
            this.controller = new CashSaleDetailController(this);
            this.controller.LoadModels(cashSaleID);

            SetFormEventHandler();
            SetFormDataSources();

        }

        #endregion

        #region Properties

        public int MyCashSaleID {
            get { return this.myCashSaleID; }
        }

        public int MyPaymentMode {
            get { return this.paymentMode; }
            set { this.paymentMode = value; }
        }

        public CashSaleModel MyModel {
            get;
            set;
        }

        public List<CashSaleDetailsModel> MyDetailsModel {
            get;
            set;
        }

        public List<TaxesModel> MyTaxesModel {
            get;
            set;
        }

        public List<CashSaleProductsModel> MyProductsModel {
            get;
            set;
        }

        public List<PaymentModeObj> MyPaymentModesModel {
            get;
            set;
        }

        public List<OrganisationsShortListObj> MyOrganisationModel {
            get;
            set;
        }
        
        public List<JobObj> MyJobsModel {
            get;
            set;
        }

        public List<TecUserObj> MyUserModel {
            get;
            set;
        }

        #endregion

        #region Methods

        public void AddDetailsModel(CashSaleDetailsModel cdm) {
            try {
                this.bsMyDetails.Add(cdm);
            } catch {
                Debug.Print("AddDetailsModel:Error");
            }
        }

        public void AddJobDetailsModel(JobObj job) {
            try { 
                this.bsJobs.Add(job);
            } catch { 
                Debug.Print("AddJobDetailsModel:Error"); 
            }
        }

        private void CreateInvoice(object sender, EventArgs e) {
            this.controller.CreateInvoice(sender, e, this.MyModel);
        }

        private void Save(object sender, EventArgs e) {
            this.controller.Save(this.MyModel);
        }

        public void SetUseDeliveryAddess(bool disOrEnabled) {
            this.deliverNameTextBox.Enabled = disOrEnabled;
            this.deliverName2TextBox.Enabled = disOrEnabled;
            this.deliverStrasseTextBox.Enabled = disOrEnabled;
            this.deliverPlzTextBox.Enabled = disOrEnabled;
            this.deliverOrtTextBox.Enabled = disOrEnabled;
        }

        public void SetFormDataSources() {
            this.bsCashSale.DataSource = this.MyModel;
            this.bsMyDetails.DataSource = this.MyModel.MyDetails;
            this.bsTaxes.DataSource = this.MyTaxesModel;
            this.bsProducts.DataSource = this.MyProductsModel;
            this.bsPaymentMode.DataSource = this.MyPaymentModesModel;
            this.invoiceNoTextBox.DataBindings[0].FormatString = DateTime.Now.Date.Year.ToString().Right(2) + "00#";
            this.bsOrganisations.DataSource = this.MyOrganisationModel;
            this.bsJobs.DataSource = this.MyModel.MyJobDetails;
            this.bsUsers.DataSource = this.MyUserModel;

            this.controller.SetSummenValues();

            if (this.MyModel != null) {
                this.MyModel.IsDirty = false;
                object myControl = this.tpDelivery;
                //bool enable = (this.MyModel.PaymentMode == 3);
                //this.controller.SelectedValueChanged(myControl, new EventArgs(), enable);
            }
            
        }

        private void SetFormEventHandler() {
            this.btnSpeichern.Click += new EventHandler(Save);
            this.btnCreateInvoice.Click += new EventHandler(CreateInvoice);
            this.bsJobs.AddingNew += new AddingNewEventHandler(this.controller.AddingNewJobs);
            this.bsMyDetails.AddingNew += new AddingNewEventHandler(this.controller.AddingNewDetails);
            this.FormClosing += new FormClosingEventHandler(this.controller.BeforeCloseMyForm);
            this.dgDetails.UserDeletingRow += new DataGridViewRowCancelEventHandler(this.controller.BeforeDeletingDetailsRow);
            this.dgDetails.UserDeletedRow += new DataGridViewRowEventHandler(this.controller.AfterDeleteDetailsRow);
        }

        public void SetFormModelDataSource() {
            this.bsCashSale.DataSource = this.MyModel;
            //this.bsJobs.DataSource = this.MyModel.MyJobDetails;
            if (this.bsJobs.Count != this.dgJobs.RowCount) {
                //this.dgJobs.Columns.Clear();
                this.dgJobs.DataSource = null;
                this.dgJobs.DataSource = this.MyModel.MyJobDetails;
                this.dgJobs.AllowUserToAddRows = true;
            }

        }

        #endregion

        #region EventHandler

        //private void bsJobsAddingNewItem(object sender, AddingNewEventArgs e) {
        //    this.controller.Jobs_AddingNew(sender, e);
        //}

        //private void bsMyDetailsAddingNewItem(object sender, AddingNewEventArgs e) {
        //    this.controller.MyDetails_AddingNew(sender, e);
        //}

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) {

        }

        private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e) {

        }

        //private void BeforeCloseForm(object sender, FormClosingEventArgs e) {
        //    e.Cancel = this.controller.BeforeCloseMyForm(sender, e);
        //}

        private void orgKeyComboBox_KeyUp(object sender, KeyEventArgs e) {
            int ind = orgKeyComboBox.SelectedIndex;
            this.controller.OrganisationsKeyUp(sender, e);
            this.bsOrganisations.DataSource = this.MyOrganisationModel;
            orgKeyComboBox.SelectedIndex = ind;
        }

        private void orgKeyComboBox_Validated(object sender, EventArgs e) {
            if (this.MyModel != null) {
                if (this.orgKeyComboBox.SelectedIndex < 0) {
                    MessageBox.Show("Kunde ist noch nicht vorhanden! Drücken Sie bitte Taste F5 oder legen den Kunde in der KIM neu an.", "Barverkauf", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                this.MyModel.OrgName = this.MyOrganisationModel[this.orgKeyComboBox.SelectedIndex].OrgName;
                this.MyModel.OrgFullName = this.MyOrganisationModel[this.orgKeyComboBox.SelectedIndex].OrgData;
            }
        }
        
        #endregion

        #region Overrides

        public override string ToString() {
            return this.Text;
        }

        #endregion































    }
}
